package uit.nfcmenu.activity;

import java.util.ArrayList;

import uit.nfcmenu.R;
import uit.nfcmenu.db.model.IDataChangeHandler;
import uit.nfcmenu.db.model.ItemModel;
import uit.nfcmenu.db.model.OrderModel;
import uit.nfcmenu.manager.AssetsManager;
import uit.nfcmenu.manager.ItemManager;
import uit.nfcmenu.manager.OrderManager;
import uit.nfcmenu.util.CommConstant;
import uit.nfcmenu.view.CookedListAdapter;
import uit.nfcmenu.webservice.MyException;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

public class ChefCookedListActivity extends BaseActivity implements IDataChangeHandler {
	
	ArrayList<OrderModel> visibleOrders;
	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cooked_list);

		initView();
		initListener();
	}

	@Override
	protected void onResume() {
		doIfMeetNetworkOrShowFailStatus(mContext, SHOW_NOTHING,
				new Runnable() { @Override public void run() {
					new RunLoader(new CheftLoader()).run();
				}
		});
		super.onResume();
	}

	public void onDataChanged() {

	}

	@Override
	protected Context getActivityContext() {
		return this;
	}

	@Override
	protected void initView() {
		super.initView();
		AssetsManager.setCommonAppFont(this, R.id.title);
		listView = (ListView) findViewById(R.id.lvCooked);
	}
	
	protected void initData() { }

	protected void initListener() { }

	protected void changeLanguage(String language) { }

	private class CheftLoader extends BaseLoader {
		
		@Override
		protected Integer doInBackground(Void... params) {
			
			ArrayList<ItemModel> items = getItems();
			if (items == null || items.size() == 0) {
				try {
					ItemManager.getInstance(mContext).updateItems();
				} catch (MyException e) {
					e.printStackTrace();
				}
			}
			
			ArrayList<OrderModel> orders = OrderManager.getInstance(mContext).getOrdersInDB();
			visibleOrders = new ArrayList<OrderModel>();
			for (int i = 0; i < orders.size(); i++) {
				OrderModel model = orders.get(i);
				if ((CommConstant.getStatus(model.getStatus()) == CommConstant.STATUS_COOKED)
						|| (CommConstant.getStatus(model.getStatus()) == CommConstant.STATUS_SERVED)
						|| (CommConstant.getStatus(model.getStatus()) == CommConstant.STATUS_DONE)) {
					visibleOrders.add(model);
				}
			}
			return super.doInBackground(params);
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			listView.setAdapter(new CookedListAdapter(mContext, visibleOrders));
		}
	}
}
